package com.runa.protocol.dto;

import com.runa.protocol.enums.ModbusFunctionCode;
import com.runa.protocol.enums.ModbusType;
import com.runa.protocol.util.ByteUtil;
import org.apache.commons.lang3.ArrayUtils;

/**
 * @author zhuj
 */
public class DtuHDRequestData extends DtuRequestData implements IRequestData {

    public DtuHDRequestData(String dtuID, IRequestData requestData){
        super(dtuID, requestData);
    }

    @Override
    public byte[] getData() {
        byte[] secOne = new byte[15];
        secOne[0] = 0x7b;
        secOne[1] = (byte) 0x89;
        int len = 16 + requestData.getData().length;
        secOne[2] = (byte) (len >> 8);
        secOne[3] = (byte) len;
        byte[] temp = dtuID.getBytes();
        System.arraycopy(temp, 0, secOne, 4, temp.length);
        byte[] secTwo = ArrayUtils.addAll(secOne, requestData.getData());
        return ArrayUtils.addAll(secTwo, new byte[]{0x7b});
    }

    public static void main(String[] args) {
        DtuHDRequestData dtuHDRequestData = new DtuHDRequestData("23030167", new ModbusReadRequestData(1, ModbusFunctionCode.READ_HOLDING_REGISTERS, 0, 2, ModbusType.MODBUS_RTU));
        System.out.println(ByteUtil.byteToHexString(dtuHDRequestData.getData()));
    }
}
